package com.sharefang.aproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.andexert.library.RippleView;
import com.balysv.materialripple.MaterialRippleLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.orhanobut.logger.Logger;
import com.sharefang.aproject.R;
import com.sharefang.aproject.bean.FolderBean;
import com.sharefang.aproject.bean.NppBean;
import com.sharefang.aproject.bean.PptBean;
import com.sharefang.aproject.constant.FragmentType;
import com.sharefang.aproject.view.MyViewHolder;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/1.
 */

public class XRecyclerViewAdapter extends XRecyclerView.Adapter<MyViewHolder> implements View.OnLongClickListener, View.OnClickListener {
    private List<PptBean> pptData = new ArrayList<>();
    private List<FolderBean> folderData = new ArrayList<>();
    private List<NppBean> nppData = new ArrayList<>();
    private FragmentType fragmentType;
    private Context mContext;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    @Override
    public boolean onLongClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemLongClick(view, Integer.parseInt((String) view.getTag()));
            return true;
        }
        return false;
    }

    @Override
    public void onClick(final View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据

            RippleView rv_root = (RippleView) view.findViewById(R.id.rv_root);
            rv_root.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                    mOnItemClickListener.onItemClick(view, Integer.parseInt((String) view.getTag()));
                }
            });
        }
    }

    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public FragmentType getFragmentType() {
        return fragmentType;
    }

    public XRecyclerViewAdapter(Context context, FragmentType fragmentType) {
        this.fragmentType = fragmentType;
        this.mContext = context;
    }

    /**
     * 设置Folder
     *
     * @param folderData
     */
    public void setFolderData(List<FolderBean> folderData) {
        if (folderData == null)
            return;
        this.folderData.clear();
        this.folderData.addAll(folderData);
    }

    /**
     * 重新设置Ppt，当刷新时
     *
     * @param pptData
     */
    public void resetPptData(List<PptBean> pptData) {
        if (pptData == null)
            return;
        this.pptData.clear();
        this.pptData.addAll(pptData);
    }

    /**
     * 设置Ppt，当加载更多时
     *
     * @param pptData
     */
    public void setPptData(List<PptBean> pptData) {
        if (pptData == null)
            return;
        this.pptData.addAll(pptData);
    }

    public void setNppData(List<NppBean> nppData) {
        if (nppData == null)
            return;
        this.nppData.addAll(nppData);
    }

    public void resetNppData(List<NppBean> nppData) {
        if (nppData == null)
            return;

        this.nppData.clear();
        this.nppData.addAll(nppData);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (fragmentType) {
            case CONTROL:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.control_item_layout, parent, false);
                break;
            case HOT:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.hot_item_layout, parent, false);
                break;
            case CONCERN:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.concern_item_layout, parent, false);
                break;
            case USER:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_npp_item_layout, parent, false);
                break;
        }
        MyViewHolder vh = new MyViewHolder(view);

        view.setOnClickListener(this);
        view.setOnLongClickListener(this);

        return vh;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.itemView.setTag("" + position);

        if (fragmentType == FragmentType.CONTROL) {

            int folderNum = folderData.size();
            int pptNum = pptData.size();
            if (folderNum != 0 && position < folderNum) {
                holder.iv_ppt.setImageResource(R.mipmap.folder);
                holder.tv_name.setText(folderData.get(position).getDirectoryName());
            } else if (pptNum != 0) {
                String thumbUrl = "http:" + pptData.get(position - folderNum).getThumbMedium();
                Picasso.with(mContext)
                        .load(thumbUrl)
                        .placeholder(R.mipmap.folder)
                        .error(R.mipmap.default_head_img)
                        .fit()
                        .centerCrop()
                        .into(holder.iv_ppt);
                holder.tv_name.setText(pptData.get(position - folderNum).getFilename());
            }

        }
        if (fragmentType == FragmentType.HOT) {
            NppBean nppBean = nppData.get(position);

            holder.tv_length.setText(nppBean.getAudioLength() + "");
            holder.tv_playTime.setText(nppBean.getPlayNum() + "");
            holder.tv_author.setText(nppBean.getNickname());
            holder.tv_time.setText(nppBean.getUploadTime() + "");
            holder.tv_name.setText(nppBean.getTitle());

            String thumbUrl = "http:" + nppBean.getThumbMedium();
            Picasso.with(mContext)
                    .load(thumbUrl)
                    .placeholder(R.mipmap.folder)
                    .error(R.mipmap.default_head_img)
                    .fit()
                    .centerCrop()
                    .into(holder.iv_ppt);
        }
        if (fragmentType == FragmentType.CONCERN) {
            NppBean nppBean = nppData.get(position);

            String headerUrl = "http:" + nppBean.getHeaderUrl();
            Picasso.with(mContext)
                    .load(headerUrl)
                    .placeholder(R.mipmap.folder)
                    .error(R.mipmap.default_head_img)
                    .fit()
                    .centerCrop()
                    .into(holder.civ_header);

            String thumbUrl = "http:" + nppBean.getThumbMedium();
            Picasso.with(mContext)
                    .load(thumbUrl)
                    .placeholder(R.mipmap.folder)
                    .error(R.mipmap.default_head_img)
                    .fit()
                    .centerCrop()
                    .into(holder.iv_ppt);

            holder.tv_author.setText(nppBean.getNickname());
            holder.tv_description.setText(nppBean.getSignature());
            holder.tv_time.setText(nppBean.getUploadTime() + "");
            holder.tv_playTime.setText(nppBean.getPlayNum() + "");
            holder.tv_nppName.setText(nppBean.getTitle());
            holder.tv_nppDescription.setText(nppBean.getDescription());
            holder.tv_share.setText(nppBean.getShareNum() + "");
            holder.tv_pinglun.setText(nppBean.getCommentNum() + "");
            holder.tv_like.setText(nppBean.getLikeNum() + "");
        }
        if (fragmentType == FragmentType.USER) {
            NppBean nppBean = nppData.get(position);

            String thumbUrl = "http:" + nppBean.getThumbMedium();
            Picasso.with(mContext)
                    .load(thumbUrl)
                    .placeholder(R.mipmap.folder)
                    .error(R.mipmap.default_head_img)
                    .fit()
                    .centerCrop()
                    .into(holder.iv_ppt);

            holder.tv_time.setText(nppBean.getUploadTime() + "");
            holder.tv_playTime.setText(nppBean.getPlayNum() + "");
            holder.tv_nppName.setText(nppBean.getTitle());
            holder.tv_length.setText(nppBean.getAudioLength() + "");
            holder.tv_nppDescription.setText(nppBean.getDescription());
            holder.tv_share.setText(nppBean.getShareNum() + "");
            holder.tv_pinglun.setText(nppBean.getCommentNum() + "");
            holder.tv_like.setText(nppBean.getLikeNum() + "");
        }

    }

    @Override
    public int getItemCount() {
        if (fragmentType == FragmentType.HOT || fragmentType == FragmentType.CONCERN || fragmentType == FragmentType.USER) {
            return nppData.size();
        }
        if (fragmentType == FragmentType.CONTROL) {
            return pptData.size() + folderData.size();
        }
        return 0;
    }


}
